// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

import {asyncRoutes, constantRoutes} from '@/router'
import * as roleApi from '@/api/roleApi.js'
import * as Auth from '@/libs/wechat';
import {formatRoutes} from '@/utils/parsing'

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = {...route}
    if (tmp.child) {
      tmp.child = filterAsyncRoutes(tmp.child, roles)
    }
    res.push(tmp)
  })

  return res
}

const state = {
  routes: [],
  addRoutes: [],
  topbarRouters: [],
  sidebarRouters: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    // state.routes = constantRoutes.concat(routes)
    state.routes = routes
  },
  SET_TOPBAR_ROUTES: (state, routes) => {
    state.topbarRouters = routes
  },
  SET_SIDEBAR_ROUTERS: (state, routes) => {
    state.sidebarRouters = routes
  },
}

const actions = {
  generateRoutes({commit}, roleid) {
    return new Promise(async resolve => {
      let accessedRoutes = []
      let menus = []
      // const { rules } = await roleApi.getRoleById(roleid) //本身注释

      //let menusAll = await roleApi.menuListApi() //await异步转同步
      let menusAll = []
      if(roleid.roles[0] === 1){
        menusAll = [
          { "id": 1, "pid": 0, "name": "首页", "icon": "s-home", "component": "/dashboard", "perms": "admin:dashboard:list", "menuType": "M", "url": "", "sort": 1, "isShow": 1, "checked": false, "childList": [] },
          { "id": 2, "pid": 0, "name": "单位部门管理", "icon": "office-building", "component": "/department/index", "perms": "admin:department:list", "menuType": "M", "url": "", "sort": 2, "isShow": 1, "checked": false, "childList": [] },
          { "id": 50, "pid": 0, "name": "通知公告", "icon": "menu", "component": "/notice/index", "perms": "admin:notice:list", "menuType": "M", "url": "", "sort": 3, "isShow": 1, "checked": false, "childList": [] },
          { "id": 3, "pid": 0, "name": "随手拍管理", "icon": "camera", "component": "/photo/index", "perms": "admin:photo:list", "menuType": "M", "url": "", "sort": 4, "isShow": 1, "checked": false, "childList": [] },
          { "id": 56, "pid": 0, "name": "督查交办", "icon": "camera-solid", "component": "/supervise/index", "perms": "admin:supervise:list", "menuType": "M", "url": "", "sort": 5, "isShow": 1, "checked": false, "childList": [] },
          { "id": 4, "pid": 0, "name": "用户管理", "icon": "user", "component": "/user/index", "perms": "admin:user:list", "menuType": "M", "url": "", "sort": 6, "isShow": 1, "checked": false, "childList": [] },
          { "id": 5, "pid": 0, "name": "系统设置", "icon": "setting", "component": "/operation", "perms": "admin:system", "menuType": "M", "url": "", "sort": 13, "isShow": 1, "checked": false, "childList": [
              { "id": 20, "pid": 5, "name": "随手拍提示配置", "icon": "", "component": "/operation/tipsConfig", "perms": "admin:system:tips:config:info", "menuType": "C", "url": "", "sort": 0, "isShow": 1, "checked": false },
              { "id": 27, "pid": 5, "name": "奖励配置", "icon": "", "component": "/operation/rewardConfig", "perms": "admin:system:reward:config:info", "menuType": "C", "url": "", "sort": 0, "isShow": 1, "checked": false },
              { "id": 22, "pid": 5, "name": "公用数据配置", "icon": "", "component": "/operation/dataConfig", "perms": "admin:dataConfig", "menuType": "C", "url": "", "sort": 0, "isShow": 1, "checked": false, },
              { "id": 12, "pid": 5, "name": "管理员列表", "icon": "", "component": "/operation/adminList", "perms": "admin:system:admin:list", "menuType": "C", "url": "/api/getUserList", "sort": 0, "isShow": 1, "checked": false },
              { "id": 7, "pid": 5, "name": "角色管理", "icon": "", "component": "/operation/identityManager", "perms": "admin:system:role:list", "menuType": "C", "url": "/api/role/getRoleList", "sort": 0, "isShow": 1, "checked": false }
            ]
          },
        ]
      }else{
        menusAll = [
          { "id": 1, "pid": 0, "name": "首页", "icon": "s-home", "component": "/dashboard", "perms": "admin:dashboard:list", "menuType": "M", "url": "", "sort": 1, "isShow": 1, "checked": false, "childList": [] },
          { "id": 3, "pid": 0, "name": "随手拍管理", "icon": "camera", "component": "/photo/index", "perms": "admin:photo:list", "menuType": "M", "url": "", "sort": 4, "isShow": 1, "checked": false, "childList": [] },
          { "id": 56, "pid": 0, "name": "督查交办", "icon": "camera-solid", "component": "/supervise/index", "perms": "admin:supervise:list", "menuType": "M", "url": "", "sort": 5, "isShow": 1, "checked": false, "childList": [] },
        ]
      }

      menusAll = formatRoutes(menusAll)

      !Auth.isPhone() ? menus = menusAll.filter(item => item.url !== '/javaMobile') : menus = menusAll.filter(item => item.url === '/javaMobile')
      const _routerResult = comRouter(menus, asyncRoutes)
      accessedRoutes = filterAsyncRoutes(_routerResult)
      // todo 这里控制是否过滤路由，经测试有些菜单不能予以设置，比如系统设置等等
      this.state.settings.showSettings = false
      commit('SET_ROUTES', menus)
      commit('SET_TOPBAR_ROUTES', menus)
      if (this.state.settings.topNav) {
        commit('SET_SIDEBAR_ROUTERS', state.sidebarRouters.length ? state.sidebarRouters : menus[0].child)
      } else {
        commit('SET_SIDEBAR_ROUTERS', menus)
      }
      // resolve(menus)
      // commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)

      // commit('SET_ROUTES', asyncRoutes)
      // resolve(asyncRoutes)
    })
  }
}

function comRouter(menus, asyncRouter, hasLeft) {
  const res = []
  asyncRouter.forEach(router => {
    const _leftUrl = hasLeft ? (hasLeft + '/' + router.path) : router.path
    const _hasPromise = menus.filter(item => item.url.startsWith(_leftUrl))
    if (_hasPromise.length > 0) {
      res.push(router)
    }
    if (router.children) {
      comRouter(menus, router.children, router.path)
    }
  })
  return res
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
